﻿Imports System.Threading

Namespace ReaderWriterLockSlimWrapper
	Public Class NHLReaderWriterLockSlim
		Inherits ReadWriteLocker

		#Region "Constructors"
			Public Sub New()
				MyBase.New(Behavior.ThrowOnDeadLocks)
			End Sub

			Public Sub New(recursionPolicy As LockRecursionPolicy)
				MyBase.New(Behavior.ThrowOnDeadLocks)
			End Sub
		#End Region

		#Region "Members"
			''' <summary>
			'''   Enters a read lock.  This is identical to calling LockForReading().
			''' </summary>
			Public Sub EnterReadLock()
				LockForReading()
			End Sub

			''' <summary>
			'''   If millisecondsTimeout is 0 then this performs a TryGetReadLock().
			'''     Otherwise, it does a regular GetReadLock, ignoring the timeout
			'''     value specified.
			''' </summary>
			''' <remarks>
			'''   Since this locking mechenism can never hard lock, timing out only
			'''     adds complexity and overhead.
			''' 
			'''   In most cases using a time out is to prevent hard locks from being
			'''     perminent.  But this leads to unneeded guess work and can defeate
			'''     thread safety.
			''' 
			'''   Using timeouts for different purposes than thread safety is not
			'''     supported, as my ReadWriteLocker is only designed to facilitate
			'''     thread safety, and that is a feature not related to thread safety.
			''' 
			'''   Please fill in a feature request on nohardlockrwlocker.codeplex.com
			'''     if you would like this feature anyways.
			''' </remarks>
			Public Sub TryEnterReadLock(millisecondsTimeout As Integer)
				If millisecondsTimeout = 0 Then
					TryLockForReading()
				Else
					LockForReading()
				End If
			End Sub

			Public Sub TryEnterReadLock(timeout As TimeSpan)
				If timeout.TotalMilliseconds = 0 Then
					TryLockForReading()
				Else
					LockForReading()
				End If
			End Sub

			''' <summary>
			'''   Enters a write lock.  This is identical to calling GetWriteLock().
			''' </summary>
			Public Sub EnterWriteLock()
				LockForWriting()
			End Sub

			''' <summary>
			'''   If millisecondsTimeout is 0 then this performs a TryGetWriteLock().
			'''     Otherwise, it does a regular GetWriteLock, ignoring the timeout
			'''     value specified.
			''' </summary>
			''' <remarks>
			'''   Since this locking mechenism can never hard lock, timing out only
			'''     adds complexity and overhead.
			''' 
			'''   In most cases using a time out is to prevent hard locks from being
			'''     perminent.  But this leads to unneeded guess work and can defeate
			'''     thWrite safety.
			''' 
			'''   Using timeouts for different purposes than thWrite safety is not
			'''     supported, as my WriteWriteLocker is only designed to facilitate
			'''     thWrite safety, and that is a feature not related to thWrite safety.
			''' 
			'''   Please fill in a feature request on nohardlockrwlocker.codeplex.com
			'''     if you would like this feature anyways.
			''' </remarks>
			Public Sub TryEnterWriteLock(millisecondsTimeout As Integer)
				If millisecondsTimeout = 0 Then
					TryLockForWriting()
				Else
					LockForWriting()
				End If
			End Sub

			''' <summary>
			'''   If millisecondsTimeout is 0 then this performs a TryGetWriteLock().
			'''     Otherwise, it does a regular GetWriteLock, ignoring the timeout
			'''     value specified.
			''' </summary>
			''' <remarks>
			'''   Since this locking mechenism can never hard lock, timing out only
			'''     adds complexity and overhead.
			''' 
			'''   In most cases using a time out is to prevent hard locks from being
			'''     perminent.  But this leads to unneeded guess work and can defeate
			'''     thWrite safety.
			''' 
			'''   Using timeouts for different purposes than thWrite safety is not
			'''     supported, as my WriteWriteLocker is only designed to facilitate
			'''     thWrite safety, and that is a feature not related to thWrite safety.
			''' 
			'''   Please fill in a feature request on nohardlockrwlocker.codeplex.com
			'''     if you would like this feature anyways.
			''' </remarks>
			Public Sub TryEnterWriteLock(timeout As TimeSpan)
				If timeout.TotalMilliseconds = 0 Then
					TryLockForWriting()
				Else
					LockForWriting()
				End If
			End Sub

			''' <summary>
			'''   This is the same as EnterReadLock().  But this also
			'''     means that you can have multiple Upgradeable Locks
			'''     at the same time.  Even if it violates the way the
			'''     old ReaderWriterLockSlim worked.
			''' </summary>
			Public Sub EnterUpgradeableReadLock()
				EnterReadLock()
			End Sub

			''' <summary>
			'''   This is the same as TryEnterReadLock().  But this also
			'''     means that you can have multiple Upgradeable Locks
			'''     at the same time.  Even if it violates the way the
			'''     old ReaderWriterLockSlim worked.
			''' </summary>
			Public Sub TryEnterUpgradeableReadLock(millisecondsTimeout As Integer)
				TryEnterReadLock(millisecondsTimeout)
			End Sub

			''' <summary>
			'''   This is the same as TryEnterReadLock().  But this also
			'''     means that you can have multiple Upgradeable Locks
			'''     at the same time.  Even if it violates the way the
			'''     old ReaderWriterLockSlim worked.
			''' </summary>
			Public Sub TryEnterUpgradeableReadLock(timeout As TimeSpan)
				TryEnterReadLock(timeout)
			End Sub

			Public Sub RestoreLock(ByRef cookie As LockCookie)
			End Sub

			Public Sub ExitUpgradeableReadLock()
				ExitReadLock()
			End Sub

			Public Sub ExitReadLock()
				UnlockForReading()
			End Sub

			Public Sub ExitWriteLock()
				UnlockForWriting()
			End Sub
		#End Region
	End Class
End Namespace
